<template>
	<view>
		<my-search @searchBoxClick="gotoSearch"></my-search>
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<block v-for="(item,i) in cateList" :key="i">
					<view :class="['left-scroll-view-item', i === active ? 'active': '']" @click="activeChange(i)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>

			<!-- 右侧的滚动视图区域 -->

			<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
				<!-- 二级分类 -->
				<view class="cate-lv2" v-for="(item2,i2) in cateLevel2" :key="i2">
					<view class="cate-lv2-title">{{item2.cat_name}}</view>
					<!-- 三级分类 -->
					<view class="cate-lv3-list">
						<view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3"
							@click="gotoGoodsList(item3)">
							<!-- 图片 -->
							<image :src="item3.cat_icon"></image>
							<!-- 文本 -->
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		mySearch
	} from "@/components/search/search"
	export default {
		components: {
			mySearch
		},
		data() {
			return {
				scrollTop: 0,
				wh: 300,
				active: 0,
				cateList: [],
				// 二级分类列表
				cateLevel2: []
			};
		},
		onLoad() {
			this.wh = uni.getSystemInfoSync().windowHeight - 50
			this.getCateList()
			// console.log(this.wh);
		},
		methods: {
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/cate/list')
				if (res.meta.status !== 200) return uni.showMsg("获取分类失败！", 1500, "error")
				this.cateList = res.message
				console.log(this.cateList);
				// 为默认的（默认选中第一个一级分类）二级分类赋值
				this.cateLevel2 = res.message[0].children
			},
			activeChange(i) {
				this.active = i
				// 点击第2个2级，二级切换为第二个的2级
				// 点击第10个1级，二级切换为第10个的2级
				this.cateLevel2 = this.cateList[i].children
				// 让scrollTop 的值在0和1之间切换
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			gotoGoodsList(item3) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/goods_search/goods_search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;
			background-color: red;

			.left-scroll-view-item {
				background: #F7F7F7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;

				// 同时包含.left-scroll-view-item和active的标签生效
				&.active {
					background-color: #FFFFFF;
					position: relative;

					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						position: absolute;
						top: 50%;
						background-color: #C00;
						// Y轴网上移动所有空间的一半
						transform: translateY(-50%);
					}
				}
			}
		}
	}

	.cate-lv2-title {
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}

	.cate-lv3-list {
		display: flex;
		// 一行排列不了，自动换行
		flex-wrap: wrap;

		.cate-lv3-item {
			// 每行控制3张图片
			width: 33.33%;
			display: flex;
			// 每个item项目中的文字和图片在垂直方向排列
			flex-direction: column;
			// 内容整体水平居中
			justify-content: center;
			// 每项内容垂直居中
			align-items: center;
			margin-bottom: 10px;

			image {
				width: 160rpx;
				height: 160rpx;
			}

			text {
				font-size: 12px;
			}
		}
	}
</style>